package com.wanxi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@Controller
@RequestMapping("/code")
/*指定请求源请求头，已经是否让客户端将凭证和跨域仪请求一起发送到服务器*/
@CrossOrigin(allowCredentials = "true",origins = "http://localhost:63342",allowedHeaders = "*")//允许跨域请求
public class CodeController {
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping
    @ResponseBody
    public void getCode(HttpServletRequest request,HttpServletResponse resp, Model model) throws IOException {
        //创建空白图片
        BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
        //1.获取图片画笔
        Graphics g = image.getGraphics();
        Random r = new Random();//随机数
        //2.设置画笔颜色
//        设置背景颜色，RGB 三个参数
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        //3. .绘制矩形的背景
        g.fillRect(0, 0, 80, 30);
        //4.调用自定义的方法,获取长度为5的字母数字组合的字符串
        String number = getNumber(5);

        //将生成的验证码放入session中

        request.getSession().setAttribute("sessionCode",number);
        redisTemplate.opsForValue().set("Code",number);

        System.out.println("生成的验证码："+number);

        g.setColor(new Color(0, 0, 0));
        g.setFont(new Font("宋体", Font.BOLD, 24));
        //5.设置颜色字体后，绘制字符串
        g.drawString(number, 5, 20);
        //6.绘制18条干扰线
        for (int i = 0; i < 18; i++) {
            g.setColor(new
                    Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), r.nextInt(255)));

            g.drawLine(r.nextInt(100),
                    r.nextInt(30),
                    r.nextInt(100),
                    r.nextInt(30));
//            直线参数 （a,b,x,y),a,b代表起点，x，y代表终点
        }
//        设置传递到前端的数据格式
        resp.setContentType("image/jpeg");
        OutputStream ops = resp.getOutputStream();
        ImageIO.write(image, "jpeg", ops);
        ops.close();

    }


    /**
     * 字符串的拼接
     *
     * @param size
     * @return
     */
    private String getNumber(int size) {

        String str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//        str.charAt(7);通过字符串下标得到对应的字符
        StringBuilder number = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < size; i++) {
            int leng = str.length();//得到字符串的长度
//            int suiji = r.nextInt();//随机数0-1之间，
            int le = r.nextInt(leng);//得到str的长度之间的随机数
            char ss = str.charAt(le);//根据charAt的下标的值，得到str对应的字符
//            需要对字符进行拼接
            number.append(ss);
//            str.charAt 拿到下标为0-str长度之间的下标，下标从0开始
//            number += str.charAt(r.nextInt(str.length()));
        }
        return number.toString();
    }
}
